// pages/blindFile/blindFile.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      // { isrequire: false, label: '家乡', key: 'homeTown', valueType: 'text', tips: '请选择', type: 'select', mode: 'region' },
      { isrequire: false, label: '籍贯', key: 'homeTown', valueType: 'text', tips: '请选择', type: 'select', mode: 'region' },
      {
        isrequire: false, label: '民族', key: 'national', rangeKey: 'name', valueType: 'text', tips: '请选择', type: 'select',data:[]
      },
      { isrequire: false, label: '属相', key: 'animalSign', valueType: 'text', tips: '请选择', type: 'select', data: ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'] },
      { isrequire: false, label: '星座', key: 'constellation', valueType: 'text', tips: '请选择', type: 'select', data: [ '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']},
      { isrequire: false, label: '血型', key: 'bloodType', valueType: 'text', tips: '请选择', type: 'select', data: ['A型', 'B型', 'AB型', 'O型', '其他'] },
      { isrequire: false, label: '是否喝酒', key: 'drinking', valueType: 'text', tips: '请填写', type: 'input'},
      { isrequire: false, label: '是否抽烟', key: 'smoking', valueType: 'text', tips: '请填写', type: 'input', },
      { isrequire: false, label: '厨艺', key: 'cooking', valueType: 'text', tips: '请填写', type: 'input', },
      { isrequire: false, label: '喜欢的运动', key: 'likeSports', valueType: 'text', tips: '请填写', type: 'input',},
      { isrequire: false, label: '喜欢的书籍', key: 'likeBookes', valueType: 'text', tips: '请填写', type: 'input'},
      { isrequire: false, label: '喜欢的电影', key: 'likeMovie', valueType: 'text', tips: '请填写', type: 'input'}
    ],
    formData: {
      homeTown: '',
      national: "",
      animalSign: '',
      constellation: '',
      bloodType: '',
      smoking: '',
      drinking: '',
      cooking: '',
      likeSports: '',
      likeBookes: '',
      likeMovie: ''
    },
    showData: {
      homeTown: '',
      national: '',
      animalSign: '',
      constellation: '',
      bloodType: ''
    },
    active: '',
    canSubmit: false,
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      // if (item.key == 'degree') {
      //   this.data.formData[item.key] = parseInt(e.detail.value) + 1;
      //   this.data.showData[item.key] = item.data[e.detail.value]
      // } else {
        if (item.data[e.detail.value].id) {
          this.data.formData[item.key] = item.data[e.detail.value].id;
          this.data.showData[item.key] = item.data[e.detail.value][item.rangeKey]
        } else {
          this.data.formData[item.key] = parseInt(e.detail.value) + 1;
          this.data.showData[item.key] = item.data[e.detail.value]
        }
      // }
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      ['formData.' + item.key]: this.data.formData[item.key],
      ['showData.' + item.key]: this.data.showData[item.key],
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    var that = this;
    wx.getStorage({
      key: 'xqArchives',
      success: function (res) {
        if (res.data) {
          for (var j in that.data.formData) {
            if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
              if (j == 'homeTown') {
                that.data.formData[j] = res.data[j].split(',');
              } else {
                if (res.data[j] == -1) {
                  that.data.formData[j] = '';
                } else {
                  that.data.formData[j] = res.data[j];
                }
              }
            }
          }
        }
        that.setData({
          formData: that.data.formData
        })
        that.getNational();
        that.setSelect('animalSign', 2, true);
        that.setSelect('constellation', 3, true);
        that.setSelect('bloodType', 4, true);
        wx.removeStorage({ key: 'xqArchives' });
      }
    })
  },
  setSelect: function (key, index, flag) {
    if (this.data.formData[key] !== '') {
      var ind = flag ? this.data.formData[key] - 1 : this.data.formData[key];
      this.setData({
        ['formlist[' + index + '].index']: ind,
        ['showData.' + key]: this.data.formlist[index].data[ind]
      })
    }
  },
  getNational: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'national/all', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {},
      success: function (res) {
        if (res.data.success) {
          res.data.data.forEach(function (ele, index) {
            if (that.data.formData.national != '') {
              if (that.data.formData.national == ele.id) {
                that.setData({
                  ['formlist[1].index']: index,
                  'showData.national': ele.name
                })
              }
            }
          })
          that.setData({
            ['formlist[1].data']: res.data.data
          })
        }
      }
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
  },
  saveBasicInfo: function (e) {
    var param = {};
    var form = e.detail.value;
    for (var j in this.data.formData) {
      if (form[j] != undefined) {
        param[j] = form[j];
        this.data.formData[j] = form[j];
      } else {
        param[j] = this.data.formData[j];
      }
    }
    if (param.homeTown != '') {
      if (param.homeTown.indexOf(',')<0){
        param.homeTown = param.homeTown.join(',');
      }
    }
    param.userId = this.data.userInfo.id;
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqArchives/update', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: param,
      success: function (res) {
        if (res.data.success) {
          if (that.data.formData.homeTown != '' && param.homeTown.indexOf(',') < 0){
            that.data.formData.homeTown = that.data.formData.homeTown.join(',');
          }
          wx.navigateBack({
            delta: 1
          })
          // wx.redirectTo({
          //   url: '/pages/blindMine/blindMine',
          // })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  saveCanel: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.brandCar == '') || (item.plateNumber == '') || (item.carColor == '') || (item.seat == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})